﻿Imports Mystery

Public Class Login

    Private Const username_welcome As String = "User Name"
    Private Const password_welcome As String = "Password"

    Public Event loginSuccefull()

    Private Sub tbUserName_GotFocus(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles tbUserName.GotFocus
        If tbUserName.Text = username_welcome Then
            tbUserName.Text = ""
        End If
    End Sub

    Private Sub UserControl_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
        tbUserName.Text = username_welcome
        tbPassword.Text = password_welcome

        'copying style
        tbRealPassword.Height = tbPassword.Height
        tbRealPassword.Width = tbPassword.Width
        tbRealPassword.VerticalAlignment = tbPassword.VerticalAlignment
        tbRealPassword.HorizontalAlignment = tbPassword.HorizontalAlignment
        tbRealPassword.Margin = tbPassword.Margin
        tbRealPassword.FontSize = tbPassword.FontSize
        tbRealPassword.FontWeight = tbPassword.FontWeight
        tbRealPassword.FontFamily = tbPassword.FontFamily
    End Sub

    Private Sub tbUserName_LostFocus(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles tbUserName.LostFocus
        If String.IsNullOrEmpty(tbUserName.Text) Then
            tbUserName.Text = username_welcome
        End If
    End Sub

    Private Sub tbPassword_GotFocus(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles tbPassword.GotFocus
        tbPassword.Visibility = Windows.Visibility.Hidden
        tbRealPassword.Visibility = Windows.Visibility.Visible
        tbRealPassword.Focus()
    End Sub


    Private Sub tbPassword_LostFocus(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles tbRealPassword.LostFocus
        If String.IsNullOrEmpty(tbRealPassword.Password) Then
            tbPassword.Visibility = Windows.Visibility.Visible
            tbRealPassword.Visibility = Windows.Visibility.Hidden
        End If
    End Sub

    Private Sub tbUserName_KeyDown(sender As System.Object, e As System.Windows.Input.KeyEventArgs) Handles tbUserName.KeyDown, tbRealPassword.KeyDown
        If Not e.Key = Key.Enter Then Exit Sub

        Dim username As String = tbUserName.Text
        Dim password As String = tbRealPassword.Password

        Dim auth As IAuthetication = getGlobalObject(Of IAuthetication)()
        Dim user As User = auth.autheticate(username, password)

        If user IsNot Nothing Then
            Dim ms As MysterySession = getGlobalObject(Of MysterySession)()
            ms.user = user
            RaiseEvent loginSuccefull()
        Else
            MsgBox("Invalid login")
        End If

    End Sub
End Class
